package com.ruoyi.college.service.impl;

import com.ruoyi.college.domain.College;
import com.ruoyi.college.domain.FirstLevelSubject;
import com.ruoyi.college.domain.SecondLevelSubject;
import com.ruoyi.college.domain.vo.SubjectTreeVo;
import com.ruoyi.college.mapper.CollegeMapper;
import com.ruoyi.college.mapper.FirstLevelSubjectMapper;
import com.ruoyi.college.mapper.SecondLevelSubjectMapper;
import com.ruoyi.college.service.ISubjectTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SubjectTreeServiceImpl implements ISubjectTreeService {

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private FirstLevelSubjectMapper firstLevelSubjectMapper;

    @Autowired
    private SecondLevelSubjectMapper secondLevelSubjectMapper;

    @Override
    public List<SubjectTreeVo> getSubjectTree() {
        // 获取所有学院
        List<College> colleges = collegeMapper.selectCollegeList(new College());

        // 获取所有一级学科并按复合键(学院ID+学科代码)分组
        List<FirstLevelSubject> firstSubjects = firstLevelSubjectMapper.selectFirstLevelSubjectList(new FirstLevelSubject());
        Map<String, FirstLevelSubject> firstSubjectMap = firstSubjects.stream()
                .collect(Collectors.toMap(
                        fs -> buildCompositeKey(fs.getCollegeId(), fs.getFirstSubjectCode()),
                        fs -> fs
                ));

        // 获取所有二级学科并按复合键(学院ID+一级学科代码)分组
        List<SecondLevelSubject> secondSubjects = secondLevelSubjectMapper.selectSecondLevelSubjectList(new SecondLevelSubject());
        Map<String, List<SecondLevelSubject>> secondSubjectMap = secondSubjects.stream()
                .collect(Collectors.groupingBy(
                        ss -> buildCompositeKey(ss.getCollegeId(), ss.getFirstSubjectId())
                ));

        // 构建树
        List<SubjectTreeVo> tree = new ArrayList<>();
        for (College college : colleges) {
            SubjectTreeVo collegeNode = new SubjectTreeVo(
                    college.getCollegeCode(),
                    college.getCollegeName(),
                    "college"
            );

            List<SubjectTreeVo> firstLevelNodes = new ArrayList<>();

            // 查找该学院的所有一级学科
            for (FirstLevelSubject firstSubject : firstSubjects) {
                if (!college.getCollegeCode().equals(firstSubject.getCollegeId())) continue;

                SubjectTreeVo firstNode = new SubjectTreeVo(
                        firstSubject.getFirstSubjectCode(),
                        firstSubject.getFirstSubjectName(),
                        "firstSubject"
                );

                // 查找该一级学科下的所有二级学科
                String compositeKey = buildCompositeKey(
                        firstSubject.getCollegeId(),
                        firstSubject.getFirstSubjectCode()
                );

                List<SecondLevelSubject> secondSubjectsList = secondSubjectMap.get(compositeKey);
                if (secondSubjectsList != null && !secondSubjectsList.isEmpty()) {
                    List<SubjectTreeVo> secondLevelNodes = secondSubjectsList.stream()
                            .map(ss -> new SubjectTreeVo(
                                    ss.getSecondSubjectCode(),
                                    ss.getSecondSubjectName(),
                                    "secondSubject"
                            ))
                            .collect(Collectors.toList());

                    firstNode.setChildren(secondLevelNodes);
                }

                firstLevelNodes.add(firstNode);
            }

            if (!firstLevelNodes.isEmpty()) {
                collegeNode.setChildren(firstLevelNodes);
            }

            tree.add(collegeNode);
        }

        return tree;
    }

    // 构建复合键：学院ID + 一级学科代码
    private String buildCompositeKey(String collegeId, String subjectCode) {
        return collegeId + "|" + subjectCode;
    }


}
